/*
  depend on curdgrid.js
  {
   tree:{
     url:'node load url'
     rootText:'' 根名称
   }
  }
*/

(function(){
  
  var treeclick="treeclick" // params: 1 scope,2 node
  Ext.fay.ui.TreeGrid=function(opt)
  {   
    opt.gridConfig=opt.gridConfig||{};
    Ext.apply( opt.gridConfig,{region:'center',border:true,margins:'2 2 2 0'})
    Ext.fay.ui.TreeGrid.superclass.constructor.call(this,opt);
    this.addEvents(treeclick);
    this.initTree(opt);
    this.initContainer(opt);    
  };  
  Ext.extend(Ext.fay.ui.TreeGrid,Ext.fay.ui.CURDGrid,{  
     initContainer:function(opt){     
      this.container=new Ext.Panel({
      border:false,      
      layout:'border',
      items:[this.grid,this.tree]      
      });     
     },
     initTree:function(opt){    
       opt.tree=opt.tree||{};
       var mine=this;
       opt.tree.listeners=opt.tree.listeners||{};
       opt.tree.listeners.click=function(node)
       {    
         mine.fireEvent(treeclick,node); 
       }    
       this.tree=new Ext.tree.TreePanel(Ext.apply({
       width:200,
       border:true,      
       region:'west',       
       split:true,      
       margins:'2 0 2 2',    
       collapsible: true,
       collapseMode:'mini', 
       rootVisible: true,
       loader: new Ext.tree.TreeLoader({dataUrl:opt.tree.url}),
       root : new Ext.tree.AsyncTreeNode({id:"0",text:opt.tree.rootText})          
       },opt.tree));     
     },
     selectedNode:function(){     
       return this.tree.getSelectionModel().getSelectedNode()||this.tree.root;
     }    
     ,
     refreshNode:function()
     {
        this.selectedNode().reload();        
     },
     
     refreshParentNode:function()
     {
        var c=this.selectedNode().parentNode||this.tree.root;  
        c.reload();       
     },
     selectedNodeID:function()
     {
       var n=this.selectedNode();
       if(n==this.tree.root)
       {
         return "";
       }
       else
       {
         return n.id;
       }
     } 
  });
  
  

})();